package com.wsecar.sharelogin;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.wsecar.sharelogin.qq.bean.QQShareParamInfo;
import com.wsecar.sharelogin.qq.bean.ShareInitConfig;
import com.wsecar.sharelogin.qq.core.QQLoginImpl;
import com.wsecar.sharelogin.qq.core.QQShareImpl;
import com.wsecar.sharelogin.qq.listener.QQImCallBackListener;
import com.wsecar.sharelogin.qq.listener.QQLoginCallBackListener;
import com.wsecar.sharelogin.qq.listener.QQMiniAppCallBackListener;
import com.wsecar.sharelogin.qq.listener.QQShareResultListener;
import com.wsecar.sharelogin.qq.listener.QQUserInfoCallBackListener;

public class ShareLoginHelper {
    private ShareInitConfig shareInitConfig;
    private QQLoginImpl qqLogin;
    private QQShareImpl qqShare;
    private ShareLoginHelper() {

    }

    private static class ShareLoginHelperHolder {
        static ShareLoginHelper helper = new ShareLoginHelper();
    }

    public static ShareLoginHelper getShareLoginHelper() {
        return ShareLoginHelperHolder.helper;
    }

    /**
     * 初始化
     *
     * @param qqAppId
     * @param qqAppKey
     */
    public void initShareInitConfig(String qqAppId, String qqAppKey) {
        shareInitConfig = new ShareInitConfig();
        shareInitConfig.setQqAppId(qqAppId);
        shareInitConfig.setQqAppKey(qqAppKey);
        qqLogin = new QQLoginImpl();
    }

    /**
     * 初始化
     */
    public void initShareInitConfigWithMiniApp(ShareInitConfig shareInitConfig) {
        this.shareInitConfig = shareInitConfig;
        qqLogin = new QQLoginImpl();
    }

    /**
     * 登录模块
     * @param activity
     * @param loginCallBackListener
     */
    public void login(Activity activity, QQLoginCallBackListener loginCallBackListener) {
        login(activity,false,loginCallBackListener,null);
    }

    /**
     * 登录模块且获取用户信息
     * @param activity
     * @param userInfoFlag
     * @param loginCallBackListener
     * @param userInfoCallBackListener
     */
    public void login(Activity activity, boolean userInfoFlag, QQLoginCallBackListener loginCallBackListener, QQUserInfoCallBackListener userInfoCallBackListener) {
        if (null == shareInitConfig || TextUtils.isEmpty(shareInitConfig.getQqAppId()) || null == activity || activity.isFinishing()) {
            return;
        }
        if (null == qqLogin){
            qqLogin = new QQLoginImpl();
        }
        qqLogin.login(activity,shareInitConfig,userInfoFlag,loginCallBackListener,userInfoCallBackListener);
    }


    public void onActivityResultData(int requestCode, int resultCode, Intent data) {
        if (null == qqLogin){
            qqLogin = new QQLoginImpl();
        }
        qqLogin.onActivityResultData(requestCode, resultCode, data);
    }

    /**
     * QQ 拉起小程序/小游戏
     * appid能够直接拉取到该应用主体对应的apptype是小程序还是小游戏
     * 此处暂时无需声明MiniApp的类型
     */
    public void  launchMiniApp(Activity activity,QQMiniAppCallBackListener listener){
        if (null == shareInitConfig || TextUtils.isEmpty(shareInitConfig.getQqAppId()) || null == activity || activity.isFinishing()) {
            return;
        }
        if (null == qqLogin){
            qqLogin = new QQLoginImpl();
        }
        qqLogin.launchMiniApp(activity,shareInitConfig, listener);
    }


    /**
     *  调用IM功能
     * @imtype  IM: Constants.IM_AIO  语音：Constants.IM_AUDIO_CHAT  视频：Constants.IM_VIDEO_CHAT
     */
    public  void  imChartWithQQ(Activity activity,String otherQQ,int imtype, QQImCallBackListener listener){
        if (null == shareInitConfig || TextUtils.isEmpty(shareInitConfig.getQqAppId()) || null == activity || activity.isFinishing()) {
            return;
        }
        if (null == qqLogin){
            qqLogin = new QQLoginImpl();
        }
        qqLogin.chartImWithQQ(activity,otherQQ,imtype,shareInitConfig,listener);
    }



    public void  qqShare(Activity activity, QQShareParamInfo shareParamInfo, QQShareResultListener listener){
        if (null == shareParamInfo || null == shareInitConfig || TextUtils.isEmpty(shareInitConfig.getQqAppId()) || null == activity || activity.isFinishing()) {
            return;
        }
        if (null == qqShare){
            qqShare = new QQShareImpl();
        }
        qqShare.qqShare(activity,shareInitConfig,shareParamInfo,listener);
    }


    public void  qqShareQzone(Activity activity, QQShareParamInfo shareParamInfo, QQShareResultListener listener){
        if (null == shareParamInfo || null == shareInitConfig || TextUtils.isEmpty(shareInitConfig.getQqAppId()) || null == activity || activity.isFinishing()) {
            return;
        }
        if (null == qqShare){
            qqShare = new QQShareImpl();
        }
        qqShare.qqShareQzone(activity,shareInitConfig,shareParamInfo,listener);
    }


    /**
     * 获取默认分享图片数据
     *  QQShare.SHARE_TO_QQ_TYPE_IMAGE
     * @return
     */
    public QQShareParamInfo builderShareParamsForImage(String imageUrl){
        QQShareParamInfo shareParamInfo = new QQShareParamInfo();
        shareParamInfo.setImageUrl(imageUrl);
        return shareParamInfo;
    }

    /**
     * 获取默认分享mp3数据
     * QQShare.SHARE_TO_QQ_TYPE_AUDIO
     * @return
     */
    public QQShareParamInfo builderShareParamsForMusicOrVideo(String appName,String title,String imageUrl,String targetUrl,String autoUrl,String summary){
        QQShareParamInfo shareParamInfo = builderShareParamsForDefault(appName,title,imageUrl,targetUrl,summary);
        shareParamInfo.setAudioUrl(autoUrl);
        return shareParamInfo;
    }


    /**
     * 获取默认分享数据
     * SHARE_TO_QQ_TYPE_DEFAULT
     * @return
     */
    public QQShareParamInfo builderShareParamsForDefault(String appName,String title,String imageUrl,String targetUrl,String summary){
        QQShareParamInfo shareParamInfo = new QQShareParamInfo();
        shareParamInfo.setAppName(appName);
        shareParamInfo.setTitle(title);
        shareParamInfo.setSummary(summary);
        shareParamInfo.setImageUrl(imageUrl);
        shareParamInfo.setTargetUrl(targetUrl);
        return shareParamInfo;
    }


    /**
     * 获取分享数据到APP
     * SHARE_TO_QQ_MINI_PROGRAM &&　SHARE_TO_QQ_TYPE_APP
     * @return
     */
    public QQShareParamInfo builderShareParamsForApp(String title,String imageUrl,String targetUrl,String summary){
        QQShareParamInfo shareParamInfo = new QQShareParamInfo();
        shareParamInfo.setTitle(title);
        shareParamInfo.setSummary(summary);
        shareParamInfo.setImageUrl(imageUrl);
        shareParamInfo.setTargetUrl(targetUrl);
        return shareParamInfo;
    }




    /**
     * 销毁对像
     */
    public void  destoryShareTencent(){
        if (null == qqShare){
            qqShare = new QQShareImpl();
        }
        qqShare.destoryShareTencent();
    }
}